//
//  AddItemViewController.m
//  Checklists
//
//  Created by ITC on 8/1/13.
//  Copyright (c) 2013 ITC. All rights reserved.
//

#import "ItemDetailViewController.h"

@interface ItemDetailViewController ()

@end

@implementation ItemDetailViewController

@synthesize doneBarButton;
@synthesize delegate, itemToEdit;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (self.itemToEdit != nil) {
        self.title = @"Edit Item";
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        for (UIView* view in cell.contentView.subviews) {
            if (view.tag == 1000) {
                UILabel *lbl = (UILabel*)view;
                [lbl setText:self.itemToEdit.text];
            }
        }

    }
}
- (void)viewWillAppear:(BOOL)animated {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    for (UIView* view in cell.contentView.subviews) {
        if (view.tag == 1000) {
            UILabel *lbl = (UILabel*)view;
            [lbl becomeFirstResponder];
        }
    }
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ItemCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    UILabel* lbl = (UILabel*)[cell viewWithTag:1000];
    [lbl setText:text];
    return cell;
}

#pragma mark - Table view delegate
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

#pragma mark - button bars
- (IBAction)cancel
{
    [self.delegate addItemViewControllerDidCancel:self];
}

- (IBAction)done
{
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    for (UIView* view in cell.contentView.subviews) {
        if (view.tag == 1000) {
            UILabel *theTextField = (UILabel*)view;
            text  = theTextField.text;
        }
    }
    ChecklistsItem *item = [[ChecklistsItem alloc] init];
    item.text = text;
    item.checked = NO;
    if (self.itemToEdit != nil) {
        [self.delegate addItemViewController:self didFinishEditingItem:item];
    } else {
        [self.delegate addItemViewController:self didFinishAddingItem:item];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    for (UIView* view in cell.contentView.subviews) {
        if (view.tag == 1000) {
            UILabel *theTextField = (UILabel*)view;
            NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
            if ([newText length] > 0) {
                self.doneBarButton.enabled = YES;
            } else {
                self.doneBarButton.enabled = NO;
            }
            return YES;
        }
    }
    return NO;
}
@end
